package www.sofn.com.sys.roles;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.sofn.com.domain.vo.SelectVO;
import www.sofn.com.sys.roles.domain.pojo.SysRolesPO;
import www.sofn.com.sys.roles.domain.query.SysRolesQuery;
import www.sofn.com.sys.roles.domain.vo.ListSysRolesVO;
import www.sofn.com.sys.roles.domain.vo.ResourceSysRolesVO;
import www.sofn.com.sys.roles.domain.vo.SysRolesVO;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SysRolesService {

    @Autowired
    private SysRolesMapper sysRolesMapper;
    public List<ListSysRolesVO> list(SysRolesQuery query) {
        final List<SysRolesPO> sysRolesPOS = sysRolesMapper.selectList(
                new LambdaQueryWrapper<SysRolesPO>()
                        .select(SysRolesPO::getId, SysRolesPO::getName, SysRolesPO::getRemarks)
                        .like(StrUtil.isNotBlank(query.getName()), SysRolesPO::getName, query.getName())
                        .orderByAsc(SysRolesPO::getName)
        );

        final List<ListSysRolesVO> listSysRolesVOS = sysRolesPOS.stream().map((v) -> {
            return BeanUtil.toBean(v, ListSysRolesVO.class);
        }).collect(Collectors.toList());
        return listSysRolesVOS;
    }

    public SysRolesVO getById(String id) {

        SysRolesPO sysRolesPO = sysRolesMapper.selectById(id);
        List<ResourceSysRolesVO> permissionResource = sysRolesMapper.findResourceByRolesId(Long.valueOf(id));
        SysRolesVO sysRolesVO = BeanUtil.toBean(sysRolesPO, SysRolesVO.class);
        List<String> resources = permissionResource.stream().map(ResourceSysRolesVO::getResourceCode).collect(Collectors.toList());
        sysRolesVO.setResources(resources);
        return sysRolesVO;
    }

    @Transactional
    public void save(SysRolesPO sysRolesPO, List<String> resources) {
        sysRolesMapper.insert(sysRolesPO);
        if (Objects.nonNull(resources) && !resources.isEmpty())
            sysRolesMapper.insertResource(resources, sysRolesPO.getId());
    }

    @Transactional
    public void update(SysRolesPO sysRolesPO, List<String> resources) {
        sysRolesMapper.updateById(sysRolesPO);
        sysRolesMapper.deleteResourceByRolesId(sysRolesPO.getId());
        if (Objects.nonNull(resources) && !resources.isEmpty())
            sysRolesMapper.insertResource(resources, sysRolesPO.getId());
    }

    public void deleteById(String id) {
        sysRolesMapper.deleteResourceByRolesId(id);
        sysRolesMapper.deleteById(id);
    }

    public List<SelectVO> findRolesPO() {
        final List<SysRolesPO> sysRolesPOS = sysRolesMapper.selectList(new LambdaQueryWrapper<SysRolesPO>()
                .select(SysRolesPO::getName, SysRolesPO::getId)
        );

        List<SelectVO> selects = sysRolesPOS.stream().map(v ->
                BeanUtil.toBean(v, SelectVO.class)
        ).collect(Collectors.toList());
        return selects;
    }
}
